﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.Packets;

namespace GameClient.Managers
{
    class ChatManager
    {
        /// <summary>
        /// Event for a message received that is for the room a user is in.
        /// </summary>
        public event Action<string, string> RoomMessage;
        /// <summary>
        /// Event for a message receieved from another user.
        /// </summary>
        public event Action<string, string> UserMessage;
        /// <summary>
        /// Event for a chat oriented response from the server.
        /// </summary>
        /// 
        public event Action<string> ChatServerMessage;
        /// <summary>
        /// Event for updating a list of users via response from the server.
        /// </summary>
        public event Action<string> UserList;
        /// <summary>
        /// Event for returning the list of rooms from a server.
        /// </summary>
        public event Action<string> RoomList;

        public ChatManager()
        {
        }

        private void UpdateRoomList(string List)
        {
            if (RoomList != null)
                RoomList(List);
        }

        private void UpdateList(string List)
        {
            if (UserList != null)
                UserList(List);
        }

        private void MessageFromServer(string Message)
        {
            if (ChatServerMessage != null)
                ChatServerMessage(Message);
        }

        private void MessageFromRoom(string From, string Message)
        {
            if (RoomMessage != null)
                RoomMessage(From, Message);
        }

        public void MessageFromUser(string From, string Message)
        {
            if (UserMessage != null)
                UserMessage(From, Message);
        }

        public void MessageRoom(string Message)
        {
            ChatPacket Payload = new ChatPacket();
            Payload.Command = ChatPacket.ChatCommand.MESSAGE_ROOM;
            Payload.Message = Message;
            Payload.Room = "Jeff's Room";

            GameClient.Managers.ClientManager.GetInstance().Send(Payload);
        }

        public void MessageUser(string To, string Message)
        {
            ChatPacket Payload = new ChatPacket();
            Payload.Command = ChatPacket.ChatCommand.MESSAGE_USER;
            Payload.Message = Message;
            Payload.To = To;

            GameClient.Managers.ClientManager.GetInstance().Send(Payload);
        }

        public void ParsePacket(ChatPacket Packet)
        {
            switch (Packet.Command)
            {
                case ChatPacket.ChatCommand.JOIN_ROOM_RESPONSE:
                    MessageFromServer(Packet.Message);
                    break;
                case ChatPacket.ChatCommand.LIST_ROOM_RESPONSE:
                    UpdateList(Packet.Message);
                    break;
                case ChatPacket.ChatCommand.MESSAGE_FROM_ROOM:
                    MessageFromRoom(Packet.From, Packet.Message);
                    break;
                case ChatPacket.ChatCommand.MESSAGE_FROM_USER:
                    MessageFromUser(Packet.From, Packet.Message);
                    break;
                case ChatPacket.ChatCommand.LIST_ROOMS_REPONSE:
                    break;
            }
        }
    }
}
